
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Admin
 */
public class DOMTest {

    public static void main(String[] args) throws ParserConfigurationException {
        Document xmldocument = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            xmldocument = builder.parse(new File("info.xml"));

            //System.out.println("<"+xmldocument.getDocumentElement().getNodeName()+">");
            NodeList nList = xmldocument.getElementsByTagName("host");
            for (int i = 0; i < nList.getLength(); i++) {
                Node node = nList.item(i);
                //System.out.println("\n<" + node.getNodeName());
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) node;
                    System.out.println("<host ip= "+ eElement.getAttribute("ip")+"/>");
                    System.out.println("<chunk name= "+ eElement.getElementsByTagName("chunk").item(1)+"/>");
                }
            }
        } catch (SAXException se) {
            //…
        } catch (ParserConfigurationException pce) {
            //…
        } catch (IOException ioe) {
            //…
        }
    }
}
